import { number } from "echarts"
import request from "../request"

// 用户操作

export const filterImagesApi = (tagsId: string[], page: number, amount: number) => {
  return request({
    url: "/api/filter-image",
    data: {
      tagsId,
      page,
      amountPerPage: amount
    },
    method: "POST"
  })
}

export const searchImagesApi = (searchInfo: string, page: number, amount: number) => {
  return request({
    url: "/api/search-image",
    method: "GET",
    params: {
      searchInfo,
      page,
      amountPerPage: amount
    }
  })
}

/**
 *
 * @param category 数据集ID
 * @param page 页码
 * @param amount 每页显示数量
 * @returns
 */
export const getImagesApi = (category: string, page: number, amount: number) => {
  return request({
    url: "/api/images",
    method: "GET",
    params: {
      category,
      page,
      amountPerPage: amount
    }
  })
}

export const getOriginImageApi = (id: string) => {
  return request({
    url: "/api/image/" + id,
    method: "GET",
    responseType: "arraybuffer"
  }).then((data : any) => {
    return (
      "data:image/jpg;base64," +
      btoa(new Uint8Array(data).reduce((data, byte) => data + String.fromCharCode(byte), ""))
    )
  })
}

export const getSimpleImageApi = (id: string) => {
  return request({
    url: "/api/simage/" + id,
    method: "GET",
    responseType: "arraybuffer"
  }).then((data : any) => {
    return (
      "data:image/jpg;base64," +
      btoa(new Uint8Array(data).reduce((data, byte) => data + String.fromCharCode(byte), ""))
    )
  })
}

export const getImageDescription = (id : string) => {
  return request({
    url: "/api/info-image",
    method: "GET",
    params: {
      id
    }
  })
}

// 管理员操作

/**
 * 
 * @param category 大类别ID
 * @param subCategory 数据集ID
 * @param description 图片描述
 * @param data 图片数据
 * @returns 
 */
export const uploadImageApi = (category:number,subCategory: number,description:string,data : FormData) => {
  data.append("category",String(category))
  data.append("sub-category",String(subCategory))
  data.append("description",description)
  return request({
    url: "/admin/api/image",
    method: "PUT",
    headers: {
      "Content-Type": "multipart/form-data"
    },
    data
  })
}

export const deleteImageApi = (id: string) => {
  return request({
    url: "/admin/api/image/" + id,
    method: "DELETE"
  })
}
